<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title"
           class="title-text">我的频道</div>
      <van-button class="edit-btn"
                  type="danger"
                  plain
                  round
                  @click="isEdit = !isEdit"
                  size="mini">
        {{ isEdit ? "完成" : "编辑" }}
      </van-button>
    </van-cell>
    <van-grid class="my-grid"
              :gutter="10">
      <van-grid-item class="grid-item"
                     v-for="(channels, index) in mychannels"
                     @click="onMyChannelClick(channels, index)"
                     :key="index">
        <van-icon slot="icon"
                  v-show="isEdit && !fiexdChannels.includes(channels.id)"
                  name="clear"></van-icon>
        <span class="text"
              slot="text"
              :class="{ active: index === active }">
          {{ channels.name }}
        </span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title"
           class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid"
              :gutter="10">
      <van-grid-item class="grid-item"
                     v-for="(channel, index) in recommentChannels"
                     :key="index"
                     icon="plus"
                     @click="onAddChannel(channel)"
                     :text="channel.name" />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import { getAllChannels,addUserChannel,deleteUserChannel } from "@/api/channel";
import {mapState} from 'vuex'
import {setItem} from '@/utils/storage'
export default {
  name: "ChannelEdit",
  components: {},
  props: {
    mychannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      allChannels: [], //所有视频
      isEdit: false, //控制编辑状态
      fiexdChannels: [0] //固定频道，不允许删除
    };
  },
  computed: {
    recommentChannels() {
      return this.allChannels.filter(channel => {
        return !this.mychannels.find(mychannels => {
          return mychannels.id === channel.id;
        });
      });
    },
    ...mapState(['user']),
  },
  watch: {},
  created() {
    this.loadAllChannels();
  },
  mounted() {},
  methods: {
    
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        // console.log(data);
        this.allChannels = data.data.channels;
      } catch (err) {
        console.log(err);
        this.$toast("获取所有频道列表失败");
      }
    },
    // 添加频道
  async  onAddChannel(channel) {
      this.mychannels.push(channel);
      // 数据持久化
      if(this.user){
        try{
          // 已登录，把数据请求接口放到主线上
          await addUserChannel({
            id:channel.id,
            seq: this.mychannels.length
          })
        }catch(err){
          this.$toast('保存失败，请稍后重试')
        }
      }else{
        setItem('TOUTIAO_CHANNELS',this.mychannels)
      }
    },
    // 切换频道
    onMyChannelClick(channels, index) {
      if (this.isEdit) {
        // 编辑状态
        // 如果是固定频道，则不删除
        if(this.fiexdChannels.includes(channels.id)){
          return
        }
        // 删除频道项
        this.mychannels.splice(index,1)
        if(index <= this.active){
          // 让激活频道的索引 -1 
          this.$emit('update-active', this.active-1,true);
        }
        // 处理持久化
        this.deleteUserChannel(channels)
      } else {
        // 非编辑状态，执行切换频道
        this.$emit("update-active", index,false);
      }
    },
    // 修改删除频道
    async deleteUserChannel(channel){
      try{
        if(this.user){
          // 已登录，则数据更新到线上
          await deleteUserChannel(channel.id)
        }else{
          // 未登录，将数据更新到本地
          setItem('TOUTIAO_CHANNELS',this.mychannels)
        }
      }catch(err){
        this.$toast('操作失败，请稍后重试')
      }
    }
  }
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
